﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;

namespace AdapterTool_Demo
{
    public partial class LogInForm : Form
    {
        private UserListTableElement m_ulte;
        private string m_user;
        private string m_pwd;
        
        // get ulte
        public UserListTableElement ULTE
        {
            get { return m_ulte; }

        }
        public LogInForm()
        {
            m_ulte = null;
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        // ok to log in
        private void button1_Click(object sender, EventArgs e)
        {
            // 1. query database for the user settings
            AccessDataBase adb = new AccessDataBase();

            if (adb.QueryUserListTable(m_user))
            {
                UserListTableElement ulte = new UserListTableElement();
                ulte = adb.LoadUserListTableElement(m_user);
                if (ulte.Password == m_pwd)
                {
                    // 2. if exist and password correct, output the ULTE
                    m_ulte = ulte;
                }
                else
                {
                    // 3. if exist but password in-correct, give warning and return
                    MessageBox.Show("Password is incorrect! Please input again.", "Warning");
                    return;
                }
            }
            else
            {
                // 4. if not exist, give warning to ask the admin for help
                MessageBox.Show("No this user. Please contact the Adminstrator for help.", "Waring");
                return;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        // cancel
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            m_user = textBox1.Text;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            m_pwd = textBox2.Text;
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                button1_Click(sender, e);
            }
        }
    }
}
